package com.qf.shop.controller;

import cn.jpush.api.push.PushResult;
import cn.jpush.api.schedule.ScheduleResult;
import com.qf.shop.annotation.ProcessInfo;
import com.qf.shop.entity.Information;
import com.qf.shop.entity.Orders;
import com.qf.shop.model.Results;
import com.qf.shop.model.TableData;
import com.qf.shop.pojo.EditInformation;
import com.qf.shop.pojo.GetInformation;
import com.qf.shop.pojo.QueryInformation;
import com.qf.shop.pojo.QueryOrder;
import com.qf.shop.service.IInformationService;
import com.qf.shop.timer.UpdateInformation;
import com.qf.shop.utils.JPushUtils;
import com.qf.shop.vo.GoodsId;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/information")
public class PushDatabaseController {

    private IInformationService iInformationService;


    public PushDatabaseController(IInformationService iInformationServiceImpl) {
        this.iInformationService = iInformationServiceImpl;
    }

    @GetMapping
    @ProcessInfo("查询所有已推送内容")
    public TableData<Information> getAllGoods(QueryInformation queryInformation,
                                         @RequestParam(defaultValue = "0") Integer offset,
                                         @RequestParam(defaultValue = "10") Integer limit){


        TableData<Information> allInformation = iInformationService.getAllOrders(queryInformation, offset, limit);
        return allInformation;

    }

    @PostMapping
    @ProcessInfo("推送内容数据")
    public Results insertInformation(GetInformation getInformation,@CookieValue("userId")String userId){

        Results results = new Results();

        if (getInformation.getDateTime() != null){
            try {

                iInformationService.insertInformation1(getInformation,userId);
                //推送
//                p()
                results.setCode(1);
                results.setMsg("success");
            }catch (Exception e){
                e.printStackTrace();
                results.setCode(-1);
                results.setMsg("fail");
            }
        }else {
            try{
                //推送
                Map<String, String> param = new HashMap<>();
                param.put("msg", getInformation.getMessage());
                param.put("title", getInformation.getTitle());
                JPushUtils.jpushAndroid(param);
                iInformationService.insertInformation(getInformation,userId);

                results.setCode(1);
                results.setMsg("success");
            }catch (Exception e){
                e.printStackTrace();
                results.setCode(-1);
                results.setMsg("fail");
            }

        }

        return results;

    }




    @GetMapping("/{id}")
    @ProcessInfo("根据id查询推送数据")
    public Information GetInformation(@PathVariable("id") Integer id){

        Information information = iInformationService.getInformation(id);
        return information;
    }

    @PutMapping
    @ProcessInfo("编辑推送内容")
    public Results ChangeInformation(@RequestBody EditInformation editInformation){

        System.out.println(editInformation.toString());
        Results results = new Results();

        try{
            iInformationService.changeInformation(editInformation);

            results.setCode(1);
            results.setMsg("success");
        }catch (Exception e){
            e.printStackTrace();
            results.setCode(-1);
            results.setMsg("fail");
        }
        return results;
    }


}
